﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FastShip88
{
    public partial class Split : System.Web.UI.Page
    {
        DBAccess.UsersDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new DBAccess.UsersDataContext();

            lblError.Visible = false;
            if (!IsPostBack)
            {
                init_data();
                div_results.Visible = true;
                div_sub.Visible = false;
                div_selected.Visible = false;

            }

        }

        protected void fvSelected_PreRender(object sender, EventArgs e)
        {
            Button btnUpdate = (Button)fvSelected.FindControl("btnUpdate");
            Button btnCancel = (Button)fvSelected.FindControl("btnCancel");
            Button btnInsert = (Button)fvSelected.FindControl("btnInsert");

            if (btnCancel == null)
            {
                div_selected.Visible = false;
                return;
            }

            if (fvSelected.CurrentMode == FormViewMode.Edit)
            {
                btnCancel.Visible = true;
                btnUpdate.Visible = true;
                btnInsert.Visible = false;
            }

            if (fvSelected.CurrentMode == FormViewMode.Insert)
            {
                btnCancel.Visible = true;
                btnUpdate.Visible = false;
                btnInsert.Visible = true;
            }
        }

        protected void init_data()
        {
            
            string strParcelNo ="";
            int userId = Utility.GetUserId(this);

            var parcels = from p in db.ParcelInfos
                          from s in db.ShopInfos from ps in db.ParcelStatus
                          where p.ShopId == s.id && p.UserId == userId && p.StatusId == ps.id &&
                            p.ParcelNo.Contains(strParcelNo) && (p.StatusId == 10 || p.StatusId == 40 || p.StatusId == 65 /*分拆取消*/ || p.StatusId == 75 /*合并取消*/  || p.StatusId == 95 /*中转请求取消*/ )
                          select new { p.Id, p.UserId, p.ParcelNo, p.ShopId, s.name, p.Description, p.Weight, p.ArriveDate, StatusName = ps.name };

            gvResults.DataSourceID = null;
            gvResults.DataSource = parcels;
            gvResults.DataBind();

        }

        protected void gvResults_SelectedIndexChanged(object sender, EventArgs e)
        {
            int parcelId = SafeConvert.ToInt(gvResults.SelectedValue);

            div_sub.Visible = true;
            div_selected.Visible = true;
            lblChange.Visible = false;
            lblCreate.Visible = true;

            gvSub.DataBind();
            fvSelected.ChangeMode(FormViewMode.Insert);
        }

     

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            e.Command.Parameters["@FromId"].Value  = gvResults.SelectedValue;
            e.Command.Parameters["@CreateDate"].Value = DateTime.Now;
            e.Command.Parameters["@IsCancel"].Value = false;
        }

        protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)
        {
            gvSub.DataBind();
            Utility.UpdateParcelStatus(SafeConvert.ToInt(gvResults.SelectedValue), 40); //40＝请求分拆

        }

       

        protected void fvSelected_ItemCommand(object sender, FormViewCommandEventArgs e)
        {
           
        }

        protected void gvSub_SelectedIndexChanged(object sender, EventArgs e)
        {
            fvSelected.ChangeMode(FormViewMode.Edit);
            //fvSelected.DataBind();
        }


        protected void gvSub_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int subParcelId = SafeConvert.ToInt( e.Keys[0]);

            var var_sp = (from s in db.SubParcels
                      where s.Id == subParcelId
                      select new { s.Id, s.FromId, s.IsCancel }).First();

            if (var_sp.IsCancel)
            {
                e.Cancel = true;
                return;
            }

            var var_p = (from p in db.ParcelInfos
                     where p.Id == var_sp.FromId
                     select new { p.StatusId }).First();
            if (var_p.StatusId != 40) //请求分拆
            {
                e.Cancel=true;
            }
        }

        protected void gvResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvResults.PageIndex = e.NewPageIndex;
            init_data();
        }

       
       
    }
}
